package com.sell.service.impl;

import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.sell.dto.OrderDTO;
import com.sell.exceptions.ResultEnum;
import com.sell.exceptions.SellException;
import com.sell.service.OrderService;
import com.sell.service.PayService;
import com.sell.utils.JsonUtil;
import com.sell.utils.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: megan
 * @Data: 2018/12/10 10:26 PM
 * @Description:
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME = "sell 订单";

    @Resource
    private BestPayServiceImpl bestPayService;
    @Resource
    private OrderService orderService;

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信支付】发起支付 请求数据 {}", JsonUtil.obj2StringPretty(payRequest));
        PayResponse response = bestPayService.pay(payRequest);
        log.info("【微信支付】发起支付 返回数据 {}", JsonUtil.obj2StringPretty(response));
        return response;
    }

    @Override
    public PayResponse notify(String notifyData) {
         /*
             1,验证签名 {sdk 已经做好了}
             2,支付状态 {sdk 已经做好了}
             3,支付金额 {需要自己做}
             4,支付的人(支付人 == 下单人) {需要自己做}
             5,修改订单的支付状态
         */
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("【微信支付】异步通知 payResponse：{}", JsonUtil.obj2StringPretty(payResponse));

        // 查询订单
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());
        if (orderDTO == null) {
            log.error("【微信支付】异步通知，订单不存在，orderId = {} ", payResponse.getOrderId());
            throw new SellException(ResultEnum.ORDER_NOT_EXIT);
        }

        // 判断金额是否一致 (0.1 == 0.100 {sdk用的接收参数是double类型，会丢失一些精度})
        if (!MathUtil.equals(orderDTO.getOrderAmount().doubleValue(), payResponse.getOrderAmount())) {
            log.error("【微信支付】异步通知,订单金额不一致, orderId={} 系统金额：{}，微信回调金额：{}", orderDTO.getOrderId(), orderDTO.getOrderAmount(), payResponse.getOrderAmount());
            throw new SellException(ResultEnum.WX_APY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        // 修改订单的支付状态
        orderService.paid(orderDTO);

        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信退款】请求参数 {} ",JsonUtil.obj2StringPretty(refundRequest));

        RefundResponse refundResponse = bestPayService.refund(refundRequest);
        log.info("【微信退款】返回参数 {} ",JsonUtil.obj2StringPretty(refundResponse));

        return refundResponse;
    }

}
